/**
 * Created by cheny on 2017/7/20.
 */
import JsSIP from 'jssip';


// Create our JsSIP instance and run it:
// var socket = new JsSIP.WebSocketInterface('wss://sip.myhost.com');

export default class SipClientManager{

    constructor(sipURI,password){
        this.connectSip=this.connectSip.bind(this);
        this.callOtherSip=this.callOtherSip.bind(this);
        this.initSipUA(sipURI,password);
    }

    initSipUA(sipURI,password){
        let socket = new JsSIP.WebSocketInterface('wss://sip.myhost.com');
        let configuration = {
            sockets  : [socket],
            uri      : sipURI,
            password : password
        };

        let ua = new JsSIP.UA(configuration);

        this.ua=ua;
    }

    connectSip(){
        this.ua.start();
    }

    callOtherSip(sipURI){
        // Register callbacks to desired call events
        let eventHandlers = {
            'progress': function(e) {
                console.log('call is in progress');
            },
            'failed': function(e) {
                console.log('call failed with cause: '+ e.data.cause);
            },
            'ended': function(e) {
                console.log('call ended with cause: '+ e.data.cause);
            },
            'confirmed': function(e) {
                console.log('call confirmed');
            }
        };

        let options = {
            'eventHandlers'    : eventHandlers,
            'mediaConstraints' : { 'audio': true, 'video': true }
        };

        let session = this.ua.call(sipURI, options);

    }

}

